package com.cloud.bc.service.impl;

import com.cloud.bc.model.entity.BcFriends;
import com.cloud.bc.mapper.BcFriendsMapper;
import com.cloud.bc.model.entity.BcSingles;
import com.cloud.bc.service.BcFriendsService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.bc.service.BcSinglesService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 朋友
 *
 * @author douhaichao code generator
 * @date 2021-01-12 10:13:07
 */
@Service
@AllArgsConstructor
public class BcFriendsServiceImpl extends ServiceImpl<BcFriendsMapper, BcFriends> implements BcFriendsService {
    private final BcSinglesService bcSinglesService;

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    @Override
    public BcFriends selectOne(BcFriends req) {
        QueryWrapper queryWrapper = new QueryWrapper<BcFriends>();
        queryWrapper.setEntity(req);
        return baseMapper.selectOne(queryWrapper);
    }

    /**
     * 根据条件查询所有
     */
    @Override
    public List<BcFriends> selectAll(BcFriends req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 根据对象查询所有分页数据(精确查询)     *      * @return
     */
    @Override
    public IPage<BcFriends> selectPage(Page page, BcFriends req) {
        IPage<BcFriends> list = baseMapper.selectPage(
                page,
                new QueryWrapper<BcFriends>().setEntity(req));
        return list;
    }

    /**
     * 根据对象查询所有分页数据(模糊查询)     *      * @return
     */
    @Override
    public IPage<BcFriends> selectPageByLike(Page page, BcFriends req) {
        QueryWrapper queryWrapper = new QueryWrapper();

        if (null != req.getId()) {
            queryWrapper.eq("id", req.getId());
        }

        if (null != req.getUserId()) {
            queryWrapper.eq("user_id", req.getUserId());
        }

        if (null != req.getTargetId()) {
            queryWrapper.eq("target_id", req.getTargetId());
        }

        if (null != req.getCreatedAt()) {
            queryWrapper.eq("created_at", req.getCreatedAt());
        }
        IPage<BcFriends> list = baseMapper.selectPage(page, queryWrapper);
        return list;
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    @Override
    public int count(BcFriends req) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(req);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public int existsFriend(Integer userId, Integer reviewUserId) {
        return baseMapper.existsFriend(userId, reviewUserId);
    }

    @Override
    public List<BcSingles> selectFriendList(Page page, Integer userId) {
        List<BcFriends> bcFriends = baseMapper.selectFriendList(page, userId);
        List<Integer> listIds = new ArrayList<>();
        for (BcFriends bcFriend : bcFriends) {
            Integer userId1 = bcFriend.getUserId();
            Integer targetId = bcFriend.getTargetId();
            if (userId1.equals(userId)) {
                //如果是自己就把targetId给到listIds里
                listIds.add(targetId);
            } else {
                listIds.add(userId1);
            }
        }
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.in("user_id", listIds);
        List<BcSingles> list = bcSinglesService.list(queryWrapper);
        return list;
    }

    @Override
    public List<BcFriends> selectFriendIds(Integer userId) {
        return baseMapper.selectFriendIds(userId);
    }
}
